package net.technics;

import java.util.ArrayList;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Familles;
import net.models.Medicaments;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.hibernate.Transaction;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MedicamentSupprimer {
	private DataBindingContext m_bindingContext;

	protected Shell	shellMedicamentSupprimer;
	private Text txtSupprimerUnMdicament;
	private Text textNom;
	private Text textComposition;
	private Text textEffets;
	private Text textContreIndication;
	private List<Familles> familles = new ArrayList<Familles>();
	private static List<Medicaments> meds = new ArrayList<Medicaments>();
	private Medicaments medicaments = new Medicaments();
	private ComboViewer comboViewerMed;
	private ComboViewer comboViewerFamille;

	private Spinner spPrixEchantillon;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					MedicamentSupprimer window = new MedicamentSupprimer();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		familles = Application.getFamillesMeds();
		meds = Application.getMeds();
		Display display = Display.getDefault();
		createContents();
		shellMedicamentSupprimer.open();
		shellMedicamentSupprimer.layout();
		while (!shellMedicamentSupprimer.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shellMedicamentSupprimer = new Shell();
		shellMedicamentSupprimer.setSize(435, 392);
		shellMedicamentSupprimer.setText("SWT Application");
		shellMedicamentSupprimer.setLayout(null);
		
		Composite composite = new Composite(shellMedicamentSupprimer, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(204, 102, 0));
		composite.setBounds(0, 0, 417, 42);
		
		txtSupprimerUnMdicament = new Text(composite, SWT.READ_ONLY);
		txtSupprimerUnMdicament.setText("Supprimer un médicament");
		txtSupprimerUnMdicament.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtSupprimerUnMdicament.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtSupprimerUnMdicament.setBackground(SWTResourceManager.getColor(204, 102, 0));
		txtSupprimerUnMdicament.setBounds(10, 10, 234, 22);
		
		Composite composite_1 = new Composite(shellMedicamentSupprimer, SWT.NONE);
		composite_1.setBounds(10, 48, 407, 37);
		
		CLabel lblMdicamentSupprimer = new CLabel(composite_1, SWT.NONE);
		lblMdicamentSupprimer.setText("Médicament à supprimer");
		lblMdicamentSupprimer.setBounds(10, 10, 172, 21);
		
		comboViewerMed = new ComboViewer(composite_1, SWT.NONE);
		Combo comboMed = comboViewerMed.getCombo();
		comboMed.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection sel = (IStructuredSelection) (comboViewerMed.getSelection());
				medicaments = (Medicaments) (sel.iterator().next());

				textNom.setText(medicaments.getName());
				comboViewerFamille.setSelection(new StructuredSelection(medicaments.getFamilles()));
				textComposition.setText(medicaments.getComposition());
				textEffets.setText(medicaments.getEffets());
				textContreIndication.setText(medicaments.getContreIndications());
				spPrixEchantillon.setSelection(medicaments.getPrixEchantillon());
			}
		});
		comboMed.setBounds(186, 5, 215, 28);
		
		Composite composite_3 = new Composite(shellMedicamentSupprimer, SWT.NONE);
		composite_3.setBounds(10, 91, 405, 206);
		
		CLabel label_1 = new CLabel(composite_3, SWT.NONE);
		label_1.setText("Nom");
		label_1.setBounds(10, 10, 39, 26);
		
		CLabel label_2 = new CLabel(composite_3, SWT.NONE);
		label_2.setText("Famille");
		label_2.setBounds(10, 42, 59, 26);
		
		CLabel label_3 = new CLabel(composite_3, SWT.NONE);
		label_3.setText("Composition");
		label_3.setBounds(10, 74, 91, 26);
		
		CLabel label_4 = new CLabel(composite_3, SWT.NONE);
		label_4.setText("Effets");
		label_4.setBounds(10, 106, 91, 26);
		
		CLabel label_5 = new CLabel(composite_3, SWT.NONE);
		label_5.setText("Contre Indications");
		label_5.setBounds(10, 138, 126, 26);
		
		CLabel label_6 = new CLabel(composite_3, SWT.NONE);
		label_6.setText("Prix Echantillon");
		label_6.setBounds(10, 170, 126, 26);
		
		textNom = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		textNom.setEnabled(false);
		textNom.setBounds(187, 10, 214, 26);
		
		textComposition = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		textComposition.setEnabled(false);
		textComposition.setBounds(187, 74, 214, 26);
		
		textEffets = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		textEffets.setEnabled(false);
		textEffets.setBounds(187, 106, 214, 26);
		
		textContreIndication = new Text(composite_3, SWT.BORDER | SWT.READ_ONLY);
		textContreIndication.setEnabled(false);
		textContreIndication.setBounds(187, 138, 214, 26);
		
		spPrixEchantillon = new Spinner(composite_3, SWT.BORDER | SWT.READ_ONLY);
		spPrixEchantillon.setEnabled(false);
		spPrixEchantillon.setBounds(187, 174, 214, 26);
		
		comboViewerFamille = new ComboViewer(composite_3, SWT.NONE);
		Combo comboFamille = comboViewerFamille.getCombo();
		comboFamille.setEnabled(false);
		comboFamille.setBounds(187, 42, 214, 28);
		
		Composite composite_2 = new Composite(shellMedicamentSupprimer, SWT.NONE);
		composite_2.setBounds(205, 297, 206, 49);
		
		Button button = new Button(composite_2, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shellMedicamentSupprimer.close();
			}
		});
		button.setText("Annuler");
		button.setBounds(106, 10, 90, 30);
		
		Button button_1 = new Button(composite_2, SWT.NONE);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
			IStructuredSelection sel = (IStructuredSelection) (comboViewerMed.getSelection());
			medicaments = (Medicaments) (sel.iterator().next());
			Medicaments medicament = medicaments;

			org.hibernate.Session session = (org.hibernate.Session) HibernateUtil.getSession();
			Transaction trans = session.beginTransaction();
			IStructuredSelection sel1 = (IStructuredSelection) (comboViewerFamille.getSelection());
			Familles familles = (Familles) (sel1.iterator().next());
			Medicaments medcs = new Medicaments(medicament.getId(), medicament, textNom.getText(), 
					familles , textComposition.getText(), textEffets.getText(), textContreIndication.getText(), 
					spPrixEchantillon.getSelection());
			session.delete(medcs);
			trans.commit();
			session.close();
			shellMedicamentSupprimer.close();
		}
	});
		button_1.setText("Valider");
		button_1.setBounds(10, 10, 90, 30);
		m_bindingContext = initDataBindings();

	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), Medicaments.class, "name");
		comboViewerMed.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		comboViewerMed.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Medicaments.class).observe(meds);
		comboViewerMed.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider_1.getKnownElements(), Familles.class, new String[]{"name", "id"});
		comboViewerFamille.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		comboViewerFamille.setContentProvider(listContentProvider_1);
		//
		IObservableList selfList_1 = Properties.selfList(Familles.class).observe(familles);
		comboViewerFamille.setInput(selfList_1);
		//
		return bindingContext;
	}
}
